package setup

import (
	"tone-agent/core"
	"tone-agent/entity"

	"github.com/fsnotify/fsnotify"
	"github.com/spf13/viper"
)

func LoadConfig() {
	viper.AddConfigPath(entity.ConfigPath)
	viper.SetConfigName(entity.APPName)
	viper.SetConfigType(entity.ConfigType)
	viper.ReadInConfig()

	viper.SetConfigName("app")
	viper.MergeInConfig()

	viper.WatchConfig()
	viper.OnConfigChange(func(e fsnotify.Event) {
		core.MyLogger.WithFields(core.LogFields{"file": e.Name}).Info(
			"Configuration file have been changed. Service is about to restart automatically")
		core.ExecCommand("systemctl", "restart", "toneagent")
	})
}
